var express = require('express');
var router = express.Router();
const { claimsModel } = require("../model/claimsModel")
const { ordersModel } = require("../model/ordersModel")
const { userModel } = require("../model/userModel")
const { insurancesModel } = require("../model/insurancesModel")


//获取所有理赔订单 状态 0为未提交理赔，1为提交后审批中，2为已完成
router.get("/getAllClaim", async (req, res) => {
  const result = await claimsModel.find().populate("insurancesCode").populate("userCode")

  try {
    res.send({
      code: 1,
      message: "获取所有理赔订单成功",
      rows: result
    })
  } catch (err) {
    res.send({
      code: 0,
      message: "获取订单失败"
    })
  }
})

//新增一个理赔申请
router.post("/addOneClaim", async (req, res) => {
  try {
    //这是全部保险信息
    const allinsur = await insurancesModel.find()
    //这是遍历后匹配的结果
    let arrResult = allinsur.filter(item => {
      return item.insurancesName.indexOf(req.body.insurancesCode) != -1
    })
    //这是替换步 userCode
    req.body.insurancesCode = arrResult[0]._id
    //这是替换用户信息为代码的步骤,全部用户信息
    const alluser = await userModel.find().populate("sellersId")
    //这是遍历后匹配的结果
    let userResult = alluser.filter(item => {
      return item.userName == req.body.userCode
    })
    //这是替换步
    console.log(userResult);
    console.log(req.body);
    req.body.userCode = userResult[0]._id
    req.body.state = 0
    console.log(req.body);

    const result = await claimsModel.create(req.body)

    res.send({
      code: 1,
      message: "提交理赔申请成功",
      rows: result
    })
  } catch (err) {
    res.send({
      code: 0,
      message: "提交理赔申请失败,用户不存在"
    })
  }
})

//模糊搜索
router.get("/searchManyClaim/:userCode", async (req, res) => {

  const result = await claimsModel.find().populate("insurancesCode").populate("userCode")
  //匹配所有理赔中用户的account
  let userResult = result.filter(item => {
    return item.userCode.userName.indexOf(req.params.userCode) != -1
  })
console.log(result);
console.log(req.params);

  try {
    res.send({
      code: 1,
      message: "获取所有理赔订单成功",
      rows: userResult
    })
  } catch (err) {
    res.send({
      code: 0,
      message: "获取订单失败"
    })
  }
})


//修改一个理赔申请 需要同时传入对应的用户的_id，可通过订单号获取
/*
    "insurancesCode": "平安",这个是保险名字，输入独特关键词即可
    "voucher": "图片名字",
    "userCode": "yiqianyun",这个是用户的账户名
*/
router.put("/modifyOneClaim", async (req, res) => {

  console.log("进入修改程序");
  //这是根据已存在的订单的信息找到保险代码并替换
  //不对，修改应该是在所有保险信息里匹配
  const allinsur = await insurancesModel.find()
  console.log(allinsur);
  //这是遍历后匹配的结果
  let arrResult = allinsur.filter(item => {
    return item.insurancesName.indexOf(req.body.insurancesCode) != -1
  })
  console.log(arrResult);
  //这是替换步 userCode
  req.body.insurancesCode = arrResult[0]._id

  // const allinsur = await ordersModel.find().populate("insurancesId")
  // console.log(allinsur);
  // //这是遍历后匹配的结果
  // let arrResult = allinsur.filter(item => {
  //   return item.insurancesId.insurancesName.indexOf(req.body.insurancesCode) != -1
  // })
  // console.log(arrResult);
  // //这是替换步 userCode
  // req.body.insurancesCode = arrResult[0]._id

  //这是根据已存在的顾客的账户名，匹配账户id并替换
  const alluser = await userModel.find().populate("sellersId")
  //这是遍历后匹配的结果
  let userResult = alluser.filter(item => {
    return item.userName == req.body.userCode
  })
  //这是替换步
  req.body.userCode = userResult[0]._id
  console.log(req.body);

  // delete req.body("")

  const result = await claimsModel.updateOne({ _id: req.body._id }, req.body)
  try {
    res.send({
      code: 1,
      message: "修改理赔订单成功",
      rows: result
    })
  } catch (err) {
    res.send({
      code: 0,
      message: "修改理赔订单失败"
    })
  }
})

//获取指定理赔信息
router.get("/getOneClaim/:_id", async (req, res) => {
  const result = await claimsModel.findById(req.params).populate("insurancesCode").populate("userCode")
  res.send({
    code: 1,
    message: "获取信息成功",
    rows: result
  })
})

//删除理赔信息(伪)
router.put("/delOneClaims/:_id", async (req, res) => {
  const result = await claimsModel.findById(req.params._id)

  result.delete = true;
  let newId = result._id
  delete result._id
  const arr = await claimsModel.updateOne({ _id: newId }, result)
  res.send({
    code: 1,
    message: "删除成功",
    rows: arr
  })
})

//批准理赔信息 params._id
router.put("/passOneClaims/:_id", async (req, res) => {
  const result = await claimsModel.findById(req.params._id)

  result.state = 2;
  let newId = result._id
  delete result._id
  const arr = await claimsModel.updateOne({ _id: newId }, result)
  res.send({
    code: 1,
    message: "操作成功，审批通过",
    rows: arr
  })
})


module.exports = router